/*
 * Copyright (c) 2012 Xilinx, Inc.  All rights reserved.
 *
 * Xilinx, Inc.
 * XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS" AS A
 * COURTESY TO YOU.  BY PROVIDING THIS DESIGN, CODE, OR INFORMATION AS
 * ONE POSSIBLE   IMPLEMENTATION OF THIS FEATURE, APPLICATION OR
 * STANDARD, XILINX IS MAKING NO REPRESENTATION THAT THIS IMPLEMENTATION
 * IS FREE FROM ANY CLAIMS OF INFRINGEMENT, AND YOU ARE RESPONSIBLE
 * FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE FOR YOUR IMPLEMENTATION.
 * XILINX EXPRESSLY DISCLAIMS ANY WARRANTY WHATSOEVER WITH RESPECT TO
 * THE ADEQUACY OF THE IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO
 * ANY WARRANTIES OR REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE
 * FROM CLAIMS OF INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

#include <stdio.h>

#include"dal_user.h"
#include "dal_kernel.h"

extern dal_op_t g_dal_op;

int irq_device_open()
{
	int ret=0;
	ret = g_dal_op.init();
	if (ret < 0)
	{
	   printf( "open device error\n");
	   return -1;
	}
	printf("open device ok\n");
	return 0;
}

int irq_device_close()
{
	int ret=0;
	ret = g_dal_op.exit();
	return ret;
}

int interrupt_set_en(unsigned int irq, unsigned int enable)
{
	int ret=0;
	ret=g_dal_op.interrupt_set_en(irq,enable);
	return ret;
}


int interrupt_register(unsigned int irq, unsigned int prio, void (* isr)(void*), void* data)
{
	int ret=0;
    ret = g_dal_op.interrupt_register(irq, prio, isr, data);
    if (ret < 0)
    {
        printf( "register interrupt failed!! irq:%d \n", irq);
        return -1;
    }
    interrupt_set_en(irq, FALSE);
    printf( "register interrupt ok!! irq:%d \n", irq);
    return 0;
}


int interrupt_unregister(unsigned int irq)
{
	int ret=0;
	interrupt_set_en(irq, FALSE);
	ret = g_dal_op.interrupt_unregister(irq);
	if (ret < 0)
	{
		printf( "register interrupt failed!! irq:%d \n", irq);
		return -1;
	}
	printf( "unregister interrupt ok!! irq:%d \n", irq);
	return 0;
}


//this is an example for using irq lib
#if 0
void hello0()
{
	printf("hello world from int1\n");
}

void hello1()
{
	printf("hello world from int3\n");
}

void hello2()
{
	printf("hello world from int4\n");
}

void hello3()
{
	printf("hello world from int5\n");
}



int main()
{
	int ret=0;
	unsigned long flag=1;

	ret=irq_device_open();
	if(ret<0)
	{
		printf("open device error\n");
		goto error;
	}
#if 1

	ret=interrupt_register(GPIO0,1,hello0,(void*)flag);
	ret=interrupt_register(GPIO1,2,hello1,(void*)flag);
	ret=interrupt_register(GPIO2,3,hello2,(void*)flag);
	ret=interrupt_register(GPIO3,4,hello3,(void*)flag);
	if(ret<0)
	{
		goto error;
	}
#endif

#if 1
	ret=interrupt_set_en(GPIO0, TRUE);
	ret=interrupt_set_en(GPIO1, TRUE);
	ret=interrupt_set_en(GPIO2, TRUE);
	ret=interrupt_set_en(GPIO3, TRUE);
	if(ret<0)
	{
		goto error;
	}
#endif

	sleep(100);


 ret=interrupt_unregister(GPIO0);
 ret=interrupt_unregister(GPIO1);
 ret=interrupt_unregister(GPIO2);
 ret=interrupt_unregister(GPIO3);
   if(ret<0)
   {
	   goto error;
   }

   ret=irq_device_close();
   if(ret<0)
   {
  	  goto error;
   }

error:
   return 0;
}
#endif
